﻿#include "guild_refresh_timer.h"

#include "TimeRefreshNtfData_struct.h"

#include "guild/guild.h"
#include "time/service_time_scheduler.hpp"
#include "proxy/guild_service_timer_proxy_mgr.h"
#include "log/service_logger.hpp"

namespace guild_service {
bool GuildRefreshTimer::start() {
  handler_ = time_schedule_many(
      5000,
      [](std::uint64_t, std::time_t ms) -> bool {
        LOG_INFO("公会定时刷新");
        TimeRefreshNtfData data;
        data.refresh_type =
            guildservicetimer_cpp::TimeRefreshType::TIME_REFRESH_TYPE_WEEK_ZERO;
        GuildServiceTimerProxyMgrSingleton->ForeachProxy(
            [&data](const role_uuid_type role_uuid,
                    const std::shared_ptr<GuildClientTimerProxy> proxy_ptr) {
              proxy_ptr->TimeRefreshNtf(data);
            });
        {
          // TODO
        }
        return true;
      },
      0);

  return handler_ != kInvalidTimeHandler;
}


bool GuildRefreshTimer::stop() {
  if (handler_ == kInvalidTimeHandler) {
    return true;
  }
  if (!time_cancel(handler_)) {
    return false;
  }
  return true;
}

bool GuildRefreshTimer::started() const {
  // TODO timer_map check valid
  return handler_ != kInvalidTimeHandler;
}

GuildRefreshTimer::~GuildRefreshTimer() {
  stop();
}

}